package com.wgchao.identityrightsmanagement.infrastructure.dao;

import com.wgchao.identityrightsmanagement.domain.organization.model.AccountOffice;
import com.wgchao.identityrightsmanagement.domain.organization.repository.AccountOfficeRepository;
import com.wgchao.identityrightsmanagement.infrastructure.convert.AccountOfficeConvert;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysBaseAccountOffice;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysBaseAccountOfficeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.UUID;

/**
 * @ClassName: AccountOfficeDao
 * @author: wgchao
 * @createTime: 2020/6/17 4:24 PM
 * @Description: TODO
 */
@Component
public class AccountOfficeDao implements AccountOfficeRepository {
    @Autowired
    private SysBaseAccountOfficeRepository accountOfficeRepository;
    @Autowired
    private AccountOfficeConvert accountOfficeConvert;

    @Override
    public String generateCode() {
        return UUID.randomUUID().toString();
    }

    @Override
    public void removeByAccount(String username) {
        accountOfficeRepository.deleteAllByUsername(username);
        accountOfficeRepository.findAllByUsername(username);
    }

    @Override
    public void removeByOffice(String officeCode) {
        accountOfficeRepository.deleteAllByOfficeCode(officeCode);
        accountOfficeRepository.findAllByOfficeCode(officeCode);
    }

    @Override
    public void removeByDepartment(String departmentCode) {
        accountOfficeRepository.deleteAllByDepartmentCode(departmentCode);
        accountOfficeRepository.findAllByDepartmentCode(departmentCode);
    }

    @Override
    public void cleanUpIllegalCorrespondence(String departmentCode, List<String> officeCodes, String operator) {
        if(officeCodes==null || officeCodes.isEmpty()){
            accountOfficeRepository.deleteAllByDepartmentCode(departmentCode);
        }else{
            accountOfficeRepository.deleteAllByDepartmentCodeAndOfficeCodeNotIn(departmentCode, officeCodes);
        }
        accountOfficeRepository.findAllByDepartmentCode(departmentCode);
    }

    @Override
    public AccountOffice findById(String s) {
        return null;
    }

    @Override
    public void remove(AccountOffice accountOffice, String operator) {

    }

    @Override
    public void add(AccountOffice accountOffice, String operator) {
        //不存在修改 只有新增
        SysBaseAccountOffice sysBaseAccountOffice = accountOfficeConvert.convert(accountOffice);
        sysBaseAccountOffice.setId(null);
        sysBaseAccountOffice = accountOfficeRepository.save(sysBaseAccountOffice);
    }

    @Override
    public void addAll(List<AccountOffice> accountOffices, String operator) {

    }

    @Override
    public void removeAll(List<AccountOffice> accountOffices, String operator) {

    }
}
